//
//  AXFMessageController.m
//  BeeQuick
//
//  Created by 王誉升 on 2016/11/19.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "AXFMessageController.h"
#import "AXFMessageModel.h"
#import "AXFMessageCell.h"

static NSString *messageCell = @"messageCellID";
@interface AXFMessageController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak) UISegmentedControl *segment;

@property (nonatomic,weak) UITableView *tabView;



@end

@implementation AXFMessageController
{
    UITableView *_tView;
    UIImageView *_imgV;
    NSArray <AXFMessageModel *> *_messageList;
    
    NSInteger _flag;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    
    [self setupUI];
    
    [self createToTableView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)setupUI
{
    UISegmentedControl *segmentC = [[UISegmentedControl alloc]initWithItems:@[@"用户消息",@"系统消息"]];
    
    segmentC.tintColor = [UIColor yellowColor];
    
    //设置宽度
    [segmentC setWidth:80 forSegmentAtIndex:0];
    [segmentC setWidth:80 forSegmentAtIndex:1];
    
    //设置默认选中
    [segmentC setSelectedSegmentIndex:0];
    
    NSDictionary *selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName: [UIColor blackColor]};
    
    [segmentC setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    
    NSDictionary *unSelectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName: [UIColor grayColor]};
    
    [segmentC setTitleTextAttributes:unSelectedTextAttributes forState:UIControlStateNormal];

    //监听事件
    [segmentC addTarget:self action:@selector(clickSegmentC:) forControlEvents:UIControlEventValueChanged];
    
    self.navigationItem.titleView = segmentC;
    
    _segment = segmentC;
    
    //自定义导航条返回按钮
    UIBarButtonItem *leftBButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"v2_goback"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToMyVC:)];
    
    self.navigationItem.leftBarButtonItem = leftBButton;
}

- (void)clickToMyVC:(UIBarButtonItem *)batBtn
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)clickSegmentC:(UISegmentedControl *)sender
{
    if (_segment.selectedSegmentIndex == 0 )
    {
        [_imgV removeFromSuperview];
        NSLog(@"被释放");
        [self createToTableView];
    }
    
    if (_segment.selectedSegmentIndex == 1)
    {
        
        [_tView removeFromSuperview];
        NSLog(@"释放了");
        [self createToImageView];
    }

}

- (void)createToTableView
{
    UITableView *tabV = [[UITableView alloc]init];
    
    [self.view addSubview:tabV];
    
    if(_flag == 0)
    {
        [tabV mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
            
        }];
        
        _flag ++;
    }
    else
    {
        [tabV mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(UIEdgeInsetsMake(64, 0, 0, 0));
            
        }];
    }
    
    tabV.delegate = self;
    tabV.dataSource = self;
    
    [self loadData];
    
    _tabView = tabV;
    _tView = tabV;
    
    [_tabView registerClass:[AXFMessageCell class] forCellReuseIdentifier:messageCell];
    
    _tView.rowHeight = 120;
    
}

- (void)loadData
{
    NSURL *url = [[NSBundle mainBundle]URLForResource:@"Message.plist" withExtension:nil];
    
    NSArray *array = [NSArray arrayWithContentsOfURL:url];
    
    NSMutableArray <AXFMessageModel *> *arrayM = [NSMutableArray array];
    
    for (NSDictionary *dict in array) {
        AXFMessageModel *model = [AXFMessageModel messageWithDict:dict];
        
        [arrayM addObject:model];
    }
    
    _messageList = arrayM.copy;
}

- (void)createToImageView
{
    UIImageView *img = [[UIImageView alloc]init];
    
    [self.view addSubview:img];
    
    img.frame = self.view.bounds;
    
    img.image = [UIImage imageNamed:@"myMessage"];
    
    _imgV = img;
    
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _messageList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AXFMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:messageCell forIndexPath:indexPath];
    
    cell.model = _messageList[indexPath.row];
    
    return cell;
}

- (void)didReceiveMemoryWarning
{
    
    [super didReceiveMemoryWarning];
    
}

@end
